/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-24
*/
package com.rzico.order.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.order.entity.VirtualProduct;
import com.rzico.order.service.VirtualProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 虚拟商品控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-07-24
 */
@Api(description = "虚拟商品接口")
@RestController
@RequestMapping("/member/virtualProduct")
public class VirtualProductMemberController extends BaseController {

    @Autowired
    private VirtualProductService virtualProductService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private MemberService memberService;

    /**
     * 分页查询虚拟商品
     *
     * @return
    */
    @ApiOperation("分页查询虚拟商品")
    @GetMapping("/list")
    @ApiImplicitParams(
            {
            @ApiImplicitParam(name = "virtualStockId", value = "仓库ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "subType", value = "类型 {0.产品,1:套餐,2:水票,3.压桶}", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "virtualType", value = "单据类型(1.买入,2.退款,3.使用,4.赠送)", dataType = "Long", paramType = "query")
    })

    public CommResult<VirtualProduct> list(Date startDate, Date endDate,Long virtualStockId,Long productId,Integer virtualType,Integer subType, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        Member member = memberService.getCurrent();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (virtualStockId!=null){
            params.put("virtualStockId", virtualStockId);
        }
        if (member!=null){
            params.put("memberId", member.getId());
        }
        if (productId!=null){
            params.put("productId", productId);
        }
        if (virtualType!=null){
            params.put("virtualType", virtualType);
        }
        if (subType!=null){
            params.put("subType", subType);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<VirtualProduct> list = virtualProductService.selectList(params);
        PageResult<VirtualProduct> pageResult = new PageResult<VirtualProduct>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }



}
